package com.likou;

import java.util.Arrays;

/**
 * Author: 陈桂明
 * Date: 2025/4/15 17:12
 * Description: q189
 */
public class q189 {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7};
        new q189().rotate(nums, 3);
        System.out.println(Arrays.toString(nums));
    }

    public void rotate(int[] nums, int k) {
        if (nums.length == k) {
            return;
        }

        k = k % nums.length;
        // rotateNums(nums, k);
        if (k <= 0) {
            return;
        }

        int[] temp = new int[k];
        int index = 0;
        for (int i = 0; i < k; i++) {
            int num = nums[nums.length - 1 - i];
            temp[index] = num;
            index++;
        }

        for (int j = nums.length - k - 1, z = 0; j >= 0; j--, z++) {
            nums[nums.length - 1 - z] = nums[j];
        }

        for (int i = 0; i < k; i++) {
            nums[k - i - 1] = temp[i];
        }
    }


    // 轮转数组
    public void rotateNums(int[] nums, int k) {
        if (k <= 0) {
            return;
        }

        int[] temp = new int[k];
        int index = 0;
        for (int i = 0; i < k; i++) {
            int num = nums[nums.length - 1 - i];
            temp[index] = num;
            index++;
        }

        for (int j = nums.length - k - 1, z = 0; j >= 0; j--, z++) {
            nums[nums.length - 1 - z] = nums[j];
        }

        for (int i = 0; i < k; i++) {
            nums[k - i - 1] = temp[i];
        }
    }
}
